/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SeleccionarPelicula.java
 *
 * Created on 19-jun-2011, 15:51:37
 */
package guiBoleteria;

import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import logica.Fabrica;
import logica.IBoleteria;
import util.datatypes.DTPelicula;


/**
 *
 * @author Administrador
 */
public class SeleccionarPelicula extends javax.swing.JDialog {

    private DefaultListModel peliculas;
    private DTPelicula pelicula;

    public SeleccionarPelicula(java.awt.Frame parent, boolean modal) {

        super(parent, modal);
        initComponents();

        cargarListaPelicula();
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        lstPeliculas = new javax.swing.JList();
        btnSeleccionar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        lstPeliculas.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        lstPeliculas.setFont(new java.awt.Font("Arial", 0, 11));
        lstPeliculas.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(lstPeliculas);

        btnSeleccionar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/pequeñoarrow_right.png"))); // NOI18N
        btnSeleccionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSeleccionarActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/cancel_icon.jpg"))); // NOI18N
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(286, Short.MAX_VALUE)
                .addComponent(btnSeleccionar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCancelar)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnSeleccionar)
                    .addComponent(btnCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSeleccionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSeleccionarActionPerformed
        if ((DTPelicula)lstPeliculas.getSelectedValue() != null) 
        {
            pelicula = (DTPelicula)lstPeliculas.getSelectedValue();
            dispose();
        } 
        else
            JOptionPane.showMessageDialog(null, "Debe seleccionar una pelicula de la lista", "Advertencia", JOptionPane.INFORMATION_MESSAGE);
}//GEN-LAST:event_btnSeleccionarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed

        pelicula = null;
        dispose();
}//GEN-LAST:event_btnCancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                SeleccionarPelicula dialog = new SeleccionarPelicula(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    public String getValue() 
    {
        this.setVisible(true);
        if (pelicula != null)
            return "" + pelicula.getId();
        else return "";
    }
    
    private void cargarListaPelicula ()
    {
        try
        {
            IBoleteria i = Fabrica.getIBoleteria();
            List<DTPelicula> listaPelicula = i.listarPeliculas();
            peliculas = new DefaultListModel();
        
            for (DTPelicula dtPelicula : listaPelicula) 
                peliculas.addElement(dtPelicula);
        
            lstPeliculas.setModel(peliculas);
        }
        catch(NullPointerException ex)
        {
            JOptionPane.showMessageDialog(null, ex.getMessage().toString(), "Error", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnSeleccionar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList lstPeliculas;
    // End of variables declaration//GEN-END:variables
}
